package io.gitee.yth.design.pattern.test.core.principle.lod;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Law of Demeter / Least Knowledge Principle
 *
 * @author yth
 * @since 2023-04-16-16:14
 */
public class LodTest1 {
    @Test
    public void test1() {
        /*
        分析SchoolManager类的直接朋友： Employee（方法返回值） CollegeManager（方法参数）
        CollegeEmployee类不是SchoolManager类的直接朋友，因为它以局部变量的方式被使用
        这违反了迪米特法制（避免出现非直接朋友的耦合）
         */
        CollegeManager collegeManager = new CollegeManager();
        SchoolManager schoolManager = new SchoolManager();
        schoolManager.printAllEmployee(collegeManager);
    }

    private static class Employee {
        private String id;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }

    private static class CollegeEmployee {
        private String id;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }

    private static class CollegeManager {
        public List<CollegeEmployee> getAllEmployee() {
            return Stream.iterate(0, i -> ++i).limit(8)
                    .map(i -> {
                        CollegeEmployee employee = new CollegeEmployee();
                        employee.setId("学院员工id=" + i);
                        return employee;
                    })
                    .collect(Collectors.toList());
        }
    }

    private static class SchoolManager {
        public List<Employee> getAllEmployee() {
            return Stream.iterate(0, i -> ++i).limit(4)
                    .map(i -> {
                        Employee employee = new Employee();
                        employee.setId("学校总部员工id=" + i);
                        return employee;
                    })
                    .collect(Collectors.toList());
        }

        public void printAllEmployee(CollegeManager sub) {
            System.out.println("-----学院员工-----");
            sub.getAllEmployee().stream()
                    .map(CollegeEmployee::getId)
                    .forEach(System.out::println);
            System.out.println("-----学校总部员工-----");
            this.getAllEmployee().stream()
                    .map(Employee::getId)
                    .forEach(System.out::println);
        }
    }

}
